package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.GetDeptIdByLoginUser;
import cn.kys.modules.system.service.LayerService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Primary
@Service
public class LayerServiceImplzxw implements LayerService {
    @Autowired
    private ToolsResposity toolsResposity;

    @Autowired
    MongoTemplate mongoTemplate;


    @Autowired
    private GetDeptIdByLoginUser getDeptIdByLoginUser;
    @Value("${adminUserIncludeStr}")
    private String adminUserIncludeStr;
    // 加部门权限的id
    public Long getDeptId() {
        return getDeptIdByLoginUser.getDeptId();
    }
    // 加部门权限的用户名
    public String getUserName() {
        return getDeptIdByLoginUser.getUserName();
    }


    @Override
    public void addLayer(String name, String cname, String url, String type) throws Exception {

        String sql_find = String.format("select * from t_layer where name='%s' or cname='%s'", name, cname);
        Map map = toolsResposity.exeSql(sql_find);
        if(map!=null) throw new Exception("数据库中已经存在该名字");
        String sql_insert = String.format("INSERT INTO public.t_layer(create_time, name, cname, type, url, dept_id) VALUES (now(),'%s', '%s', '%s', '%s',%d)", name, cname, type, url, getDeptId());
        toolsResposity.exeSql(sql_insert);

    }

    @Override
    public Map editLayer(String id, String name, String cname, String url, String type) {
        Map resultMap = new HashMap();
        List<Map> nameList = toolsResposity.queryNameFromLayer();
        for (Map map : nameList) {
            //判断更新的是不是同一条，是的话
            String idStr = map.get("id").toString();
            String nameStr = map.get("cname").toString();
            if (!id.equals(idStr) && name.equals(nameStr)) {
                resultMap.put("success", false);
                resultMap.put("msg", name + "已经存在");
                return resultMap;
            }
        }

        String sqlUpdate = String.format("UPDATE public.t_layer SET create_time=now(), name='%s', cname='%s', type='%s', url='%s' WHERE id=%d", name, cname,type, url, Integer.parseInt(id));
        toolsResposity.exeSql(sqlUpdate);

        resultMap.put("success",true);
        resultMap.put("msg", "编辑成功");
        return resultMap;
    }


    @Override
    public List getAllLayers() {
        String sql = "";
        if (getUserName().contains(adminUserIncludeStr)) {
            sql = String.format("SELECT * FROM public.t_layer");
        } else {
            sql = String.format("SELECT * FROM public.t_layer where dept_id=%d", getDeptId());
        }
        return toolsResposity.exeSqlList(sql);

    }

    @Override
    public Map getAllTypeLayers() {
        Map map = new HashMap();

        String sqlDt = "";
        String sqlZtt = "";
        String sqlGzt = "";
        if (getUserName().contains(adminUserIncludeStr)) {
            sqlDt = "SELECT * FROM public.t_layer where type = '底图'";
            sqlZtt = "SELECT * FROM public.t_layer where type = '专题图'";
            sqlGzt = "SELECT * FROM public.t_layer where type = '工作图'";

        } else {

            sqlDt = String.format("SELECT * FROM public.t_layer where type = '底图' and dept_id=%d", getDeptId());
            sqlZtt = String.format("SELECT * FROM public.t_layer where type = '专题图' and dept_id=%d", getDeptId());
            sqlGzt = String.format("SELECT * FROM public.t_layer where type = '工作图' and dept_id=%d", getDeptId());

        }
        List listDT = toolsResposity.exeSqlList(sqlDt);
        List listZTT = toolsResposity.exeSqlList(sqlZtt);
        List listGZT = toolsResposity.exeSqlList(sqlGzt);
        map.put("dt", listDT);
        map.put("ztt", listZTT);
        map.put("gzt", listGZT);

        return map;
    }

    @Override
    public Map delLayerById(String id) {

        Map returnMap = new HashMap();

        String sqlLayer = "select * from t_layer where id = '" + id + "'";
        Map layerMap = toolsResposity.exeSql(sqlLayer);
        String type = layerMap.get("type").toString();

        String sql = "";

        if ("底图".equals(type)) {
            //底图
            sql = "select * from t_case_baselayer where layerid in (" + id + ")";
            List caseLayerMap = toolsResposity.exeSqlList(sql);
            if (caseLayerMap.size() > 0) {
                //不能删
                returnMap.put("deleted", false);
                returnMap.put("msg", "方案表中使用了该图层，不能删除");
            } else {
                //可以删除
                String sql_delete = "delete from t_layer where id in (" + id + ")";
                toolsResposity.exeSql(sql_delete);
                returnMap.put("deleted", true);
            }
        } else if ("专题图".equals(type)) {
            //专题图
            sql = "select * from t_case_speciallayer where layerid in (" + id + ")";
            List caseLayerMap = toolsResposity.exeSqlList(sql);
            if (caseLayerMap.size() > 0) {
                //不能删
                returnMap.put("deleted", false);
                returnMap.put("msg", "方案表中使用了该图层，不能删除");
            } else {
                //可以删除
                String sql_delete = "delete from t_layer where id in (" + id + ")";
                toolsResposity.exeSql(sql_delete);
                returnMap.put("deleted", true);
            }
        } else {
            //工作图
            sql = "select * from t_case_editlayer where layerid in (" + id + ")";
            List caseLayerMap = toolsResposity.exeSqlList(sql);
            if (caseLayerMap.size() > 0) {
                //不能删
                returnMap.put("deleted", false);
                returnMap.put("msg", "方案表中使用了该图层，不能删除");
            } else {
                //可以删除
                String sql_delete = "delete from t_layer where id in (" + id + ")";
                toolsResposity.exeSql(sql_delete);
                returnMap.put("deleted", true);
            }
        }


        return returnMap;
    }
}

